﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmQuanLyDiemNghe : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "diem_hoc";
        private bool mblnIsValid = false;

        private int mintTrinhDo;
        private int mintKDT;
        private int mintLopID;
        private int mintMonID;
        private int mintSoDiemDinhKy;
        private string mstrMon;

        public frmQuanLyDiemNghe()
        {
            InitializeComponent();
        }

        private void frmQuanLyDiemNghe_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mKhungDaoTao_GetFullWithoutLop' table. You can move, or remove it, as needed.
            this.mKhungDaoTao_GetFullWithoutLopTableAdapter.Fill(this.qLTHDataSet.mKhungDaoTao_GetFullWithoutLop);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhungDaoTao_GetFull' table. You can move, or remove it, as needed.
            this.mKhungDaoTao_GetFullTableAdapter.Fill(this.qLTHDataSet.mKhungDaoTao_GetFull);
        }

        private void grdControlLop_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                mintKDT = clsCommon.fncCnvNullToInt(dgvLop.GetFocusedRowCellValue(colKDT_ID));

                if (mintKDT <= 0) return;

                Cursor.Current = Cursors.WaitCursor;

                mintTrinhDo = clsCommon.fncCnvNullToInt(dgvLop.GetFocusedRowCellValue(colTD_ID));
                mintLopID = clsCommon.fncCnvNullToInt(dgvLop.GetFocusedRowCellValue(colLop_ID));
                mintMonID = clsCommon.fncCnvNullToInt(dgvLop.GetFocusedRowCellValue(colMO_ID));
                mstrMon = clsCommon.fncCnvNullToString(dgvLop.GetFocusedRowCellValue(colmo_tenmon));

                grpMonHoc.Text = mstrMon;

                // TODO: This line of code loads data into the 'qLTHDataSet.tHocSinhMon_GetFull' table. You can move, or remove it, as needed.
                this.tHocSinhMon_GetFullTableAdapter.Fill(this.qLTHDataSet.tHocSinhMon_GetFull, mintKDT, mintLopID, mintMonID);

                //ẩn cột thừa
                DTO.MKhungDaoTaoMonDTO objKdtMon = DAO.MKhungDaoTaoMonDAO.Select(Program.gobjDb, mintKDT, mintMonID);
                for (int i = 1; i <= 10; i++)
                {
                    string strColName = "HSM_diemdinhkylanso";
                    strColName += i.ToString();

                    if (i <= objKdtMon.SoDiemDinhKi)
                    {
                        dgvDiem.Columns[strColName].Visible = true;
                        continue;
                    }

                    dgvDiem.Columns[strColName].Visible = false;
                }

                mintSoDiemDinhKy = objKdtMon.SoDiemDinhKi;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void grdControlMon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                mintKDT = clsCommon.fncCnvNullToInt(dgvMon.GetFocusedRowCellValue(colKDT_ID1));

                if (mintKDT <= 0) return;

                Cursor.Current = Cursors.WaitCursor;

                //mintLopID = clsCommon.fncCnvNullToInt(dgvLop.GetFocusedRowCellValue(colLop_ID1));
                mintLopID = clsConst.gcintNoneValue;
                mintMonID = clsCommon.fncCnvNullToInt(dgvMon.GetFocusedRowCellValue(colMO_ID1));
                mstrMon = clsCommon.fncCnvNullToString(dgvMon.GetFocusedRowCellValue(colmo_tenmon1));

                grpMonHoc.Text = mstrMon;

                // TODO: This line of code loads data into the 'qLTHDataSet.tHocSinhMon_GetFull' table. You can move, or remove it, as needed.
                this.tHocSinhMon_GetFullTableAdapter.Fill(this.qLTHDataSet.tHocSinhMon_GetFull, mintKDT, mintLopID, mintMonID);

                //ẩn cột thừa
                DTO.MKhungDaoTaoMonDTO objKdtMon = DAO.MKhungDaoTaoMonDAO.Select(Program.gobjDb, mintKDT, mintMonID);
                for (int i = 1; i <= 10; i++)
                {
                    string strColName = "HSM_diemdinhkylanso";
                    strColName += i.ToString();

                    if (i <= objKdtMon.SoDiemDinhKi)
                    {
                        dgvDiem.Columns[strColName].Visible = true;
                        continue;
                    }

                    dgvDiem.Columns[strColName].Visible = false;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvDiem.UpdateCurrentRow();
                if (!mblnIsValid) return;

                Cursor.Current = Cursors.WaitCursor;

                DTO.THocSinhMonDTO objHsMon;
                bool blnSuccess = true;
                int intHsID;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    for (int i = 0; i <= dgvDiem.RowCount - 1; i++)
                    {
                        if (!blnSuccess) break;

                        intHsID = clsCommon.fncCnvNullToInt(dgvDiem.GetRowCellValue(i, colHS_ID));
                        objHsMon = DAO.THocSinhMonDAO.Select(Program.gobjDb, intHsID, mintMonID);
                        objHsMon.HSM_diemdinhkylanso1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso1));
                        objHsMon.HSM_diemdinhkylanso2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso2));
                        objHsMon.HSM_diemdinhkylanso3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso3));
                        objHsMon.HSM_diemdinhkylanso4 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso4));
                        objHsMon.HSM_diemdinhkylanso5 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso5));
                        objHsMon.HSM_diemdinhkylanso6 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso6));
                        objHsMon.HSM_diemdinhkylanso7 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso7));
                        objHsMon.HSM_diemdinhkylanso8 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso8));
                        objHsMon.HSM_diemdinhkylanso9 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso9));
                        objHsMon.HSM_diemdinhkylanso10 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso10));

                        objHsMon.HSM_diemketthucmodulelan1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemketthucmodulelan1));
                        objHsMon.HSM_diemketthucmodulelan2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemketthucmodulelan2));

                        objHsMon.HSM_diemtongket = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemtongket));
                        objHsMon.HSM_ghichu = clsCommon.fncCnvNullToString(dgvDiem.GetRowCellValue(i, colHSM_ghichu));

                        blnSuccess = blnSuccess && DAO.THocSinhMonDAO.Update(Program.gobjDb, Program.gobjTrans, objHsMon);
                    }

                    //commit dữ liệu nếu không có gì lỗi, ngược lại thì rollback
                    if (blnSuccess) { Program.gobjTrans.Commit(); }
                    else
                    { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess) { clsCommon.fncMsgInfo("Cập nhật thành công."); }
                else { clsCommon.fncMsgError("Cập nhật thất bại. Xin hãy thử lại."); }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvDiem_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                for (int i = 1; i <= 10; i++)
                {
                    string strColName = "HSM_diemdinhkylanso";
                    float fltDiem = 0.0F;
                    strColName += i.ToString();
                    if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }

                    if (fltDiem < 0 || fltDiem > 10)
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }

                    mblnIsValid = true;
                }

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvDiem_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            try
            {
                e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnTinhDiemTongKet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DTO.THocSinhMonDTO objHsMon;
                List<float> lstDiemDinhKy;
                float fltDiemKetThuc;

                objHsMon = new DTO.THocSinhMonDTO();
                lstDiemDinhKy = new List<float>();

                for (int i = 0; i <= dgvDiem.RowCount - 1; i++)
                {
                    lstDiemDinhKy.Clear();

                    objHsMon.HSM_diemdinhkylanso1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso1)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso1);
                    objHsMon.HSM_diemdinhkylanso2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso2)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso2);
                    objHsMon.HSM_diemdinhkylanso3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso3)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso3);
                    objHsMon.HSM_diemdinhkylanso4 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso4)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso4);
                    objHsMon.HSM_diemdinhkylanso5 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso5)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso5);
                    objHsMon.HSM_diemdinhkylanso6 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso6)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso6);
                    objHsMon.HSM_diemdinhkylanso7 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso7)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso7);
                    objHsMon.HSM_diemdinhkylanso8 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso8)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso8);
                    objHsMon.HSM_diemdinhkylanso9 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso9)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso9);
                    objHsMon.HSM_diemdinhkylanso10 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemdinhkylanso10)); lstDiemDinhKy.Add(objHsMon.HSM_diemdinhkylanso10);

                    objHsMon.HSM_diemketthucmodulelan1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemketthucmodulelan1));
                    objHsMon.HSM_diemketthucmodulelan2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colHSM_diemketthucmodulelan2));

                    fltDiemKetThuc = objHsMon.HSM_diemketthucmodulelan1;
                    if (objHsMon.HSM_diemketthucmodulelan2 > objHsMon.HSM_diemketthucmodulelan1) fltDiemKetThuc = objHsMon.HSM_diemketthucmodulelan2;

                    objHsMon.HSM_diemtongket = clsCommon.fncDiemTongKetMonNghe((clsEnum.TRINH_DO_DAO_TAO)mintTrinhDo, lstDiemDinhKy, mintSoDiemDinhKy, fltDiemKetThuc);

                    dgvDiem.SetRowCellValue(i, colHSM_diemtongket, objHsMon.HSM_diemtongket);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
        
    }
}